<template>
	<view class="page">
		<custom-navigation :title="title" rightTxt="保存" @rightAction="save">
		</custom-navigation>
		<scroll-view class="body-scroll" scroll-y="true">
			<view class="title flex-row-center">
				<view class="line1"></view>
				<text>基本信息</text>
			</view>
			<view class="item-contianer flex-column">
				<view class="imgs flex-row-center">
					<view v-for="(item,index) in imgs" style="position: relative;">

						<image :src="item" @click="imgsClick(index,imgs)"></image>
						<image src="/static/remove.png" class="img-del" @click="e=>{
							imgs.splice(index,1);
						}"></image>
					</view>
					<image v-show="imgs.length==0" src="/static/camera.png" @click="addImage()"></image>
				</view>
				<view class="item flex-row-center">

					<view class="title">商品条码 <text class="x">*</text></view>

					<view class="qrinput input-hint" v-if="goods_data!=null">{{goods_barcode}}</view>
					<input v-if="goods_data==null" class="qrinput" placeholder="扫码或录入条码" placeholder-class="input-hint"
						v-model="goods_barcode" />
					<image src="/static/qrcode.png" class="qrimg" @click="qrcodeClick" v-if="goods_data==null"></image>
					<view class="btn" @click="autoBarcode" v-if="goods_data==null">自动生成</view>
				</view>
				<view class="item flex-row-center">
					<view class="title">商品名称 <text class="x">*</text> </view>

					<input placeholder="请输入" placeholder-class="input-hint" v-model="goods_name" />
				</view>
				<view class="item flex-row-center" @click="goodsClassifySelect">
					<view class="title">商品分类 <text class="x">*</text></view>

					<view class="value">{{goods_classify.classify_name}}
						<image src="../../static/arrow-blackmin.png" mode="heightFix"></image>
					</view>
				</view>
				<view class="item flex-row-center">
					<view class="title">初始库存 <text class="x">*</text></view>
					<view class="qrinput input-hint" v-if="goods_data!=null">{{initial_stock}}</view>
					<input placeholder="请输入" v-if="goods_data==null" placeholder-class="input-hint" type="number"
						v-model="initial_stock" />
				</view>
				<view class="item flex-row-center">
					<view class="title">销售价格 <text class="x">*</text></view>

					<input class="qrinput" placeholder="请输入" placeholder-class="input-hint" type="digit"
						v-model="sale_price" />
					<view class="unit">元</view>
				</view>
				<view class="item flex-row-center">
					<view class="title">进货价</view>
					<input placeholder="请输入" placeholder-class="input-hint" type="digit" v-model="purchase_price" />
					<!-- purchase_price=e.detail.value.replace(/[^0-9.]/g,' ') -->




					<view class="unit">元</view>
				</view>
				<view class="item flex-row-center" @click="$navigateTo('/pages/goods/goods_unit?selectMode=true')">
					<view class="title">商品单位 </view>
					<view class="value">{{goods_unit.unit_name}}
						<image src="../../static/arrow-blackmin.png" mode="heightFix"></image>
					</view>
				</view>
				<view class="item flex-row-center" @click="$navigateTo('/pages/goods/goods_supplier?selectMode=true')">
					<view class="title">供应商 </view>
					<view class="value">{{supplier.supplier_name}}
						<image src="../../static/arrow-blackmin.png" mode="heightFix"></image>
					</view>
				</view>
				<view class="item flex-row-center">
					<view class="title">商品类型 </view>
					<view class="radio-group flex-row">
						<view v-for="item in splxList" class="radio" @click="itemRadioCheck(item,splxList)">
							<image :src="item.check?'/static/check2.png':'/static/uncheck.png'"></image>
							<text>{{item.name}}</text>
						</view>
					</view>
				</view>
				<view class="item flex-row-center" v-if="splxList[1].check">
					<view class="title">套餐/组合商品 </view>
					<view style="flex: 1;"></view>
					<view class="mealedit" @click="toMeal()">编辑/新增</view>
				</view>
				<view class="meal-box" v-if="splxList[1].check">
					<view v-if="setMeal.length>0" v-for="item in setMeal" class="value">
						{{item.goods_name+" "}}+{{item.goods_num}}
					</view>
					<view class="empty" v-if="setMeal.length==0" @click="$navigateTo('/pages/goods/set_meal')">
						去搭配套餐/组合商品</view>

				</view>
				<view class="item flex-row-center">
					<view class="title">计价方式 </view>
					<view class="radio-group flex-row">
						<view v-for="item in jjfsList" class="radio" @click="itemRadioCheck(item,jjfsList)">
							<image :src="item.check?'/static/check2.png':'/static/uncheck.png'"></image>
							<text>{{item.name}}</text>
						</view>
					</view>
				</view>
			</view>
			<view class="title flex-row-center  ">
				<view class="line1"></view>
				<text>会员优惠</text>
			</view>
			<view class="item-contianer flex-column">

				<view class="item-vip flex-row-center">
					<view class="title" style="flex: 1;">是否启用会员价 </view>
					<image class="vip-switch" :src="isVipPrice?'/static/switch-on.png':'/static/switch-off.png'"
						@click="switchVipPrice"> </image>

				</view>
				<view class="vip-price-box flex-column" v-if="isVipPrice">
					<view class="flex-row">
						<view class="item1">会员等级名称</view>
						<view class="item1">系统会员折扣(%）</view>
						<view class="item1">会员优惠价（元）</view>
					</view>
					<view class="flex-row" v-for="(item,index) in vipPriceData">
						<view class="item1">{{item.member_level_name}}</view>
						<view class="item1">{{item.discount_rate}}</view>
						<view v-if="item.member_price_type=='禁用' || item.member_price_dis=='禁用'" class="item1">{{item.member_discount_amount}}</view>
						<input v-else placeholder-class="input-hint" placeholder="请输入" type="digit"
							v-model="item.member_discount_amount" v-enter-digit    @input="e=> {
								e.target.value =  e.target.value.match(/^\d*(\.?\d{0,2})/g)[0] || ''
								e.target.value =Math.abs(e.target.value )
								  $nextTick(() => {
									item.member_discount_amount= e.target.value
								  })   
							}" />
					</view>

				</view>

			</view>

			<view class="title flex-row-center  ">
				<view class="line1"></view>
				<text>销售信息</text>
			</view>
			<view class="item-contianer flex-column">

				<!-- <view class="item flex-row-center">
					<view class="title">供应商 </view>
					<view class="check-box-group">

					</view>
				</view> -->


				<view class="item flex-row-center">
					<view class="title">售卖状态 </view>
					<view class="radio-group flex-row">
						<view v-for="item in smfsList" class="radio" @click="itemRadioCheck(item,smfsList)">
							<image :src="item.check?'/static/check2.png':'/static/uncheck.png'"></image>
							<text>{{item.name}}</text>
						</view>
					</view>
				</view>
				<view class="item flex-row">
					<view class="title">收银端设置: </view>
					<view class="check-box-group flex-column">
						<view v-for="item in cashier_setup" class="check-box1" @click="itemCheckbox(item)">
							<image :src="item.check?'/static/check3.png':'/static/uncheck3.png'"></image>
							<text>{{item.name}}</text>
						</view>
					</view>
				</view>

				<view class="item-vip flex-row-center">
					<view class="title" style="flex: 1;">是否允许低于最低售价 </view>
					<image class="vip-switch"
						:src="allow_min_sales_price=='允许'?'/static/switch-on.png':'/static/switch-off.png'"
						@click="switchallow_min_sales_price"> </image>

				</view>

			</view>
			<view class="title flex-row-center">
				<view class="line1"></view>
				<text>商品拓展信息</text>
				<view :class="!extensionShow?'show1':'hide1'" @click="extensionShow=false">隐藏</view>
				<view :class="extensionShow?'show2':'hide2'" @click="extensionShow=true">显示</view>
			</view>

			<view class="item-contianer flex-column" v-if="extensionShow">



				<view class="item flex-row-center">
					<view class="title">划线价 </view>

					<input class="qrinput" placeholder="请输入" placeholder-class="input-hint" type="digit"
						v-model="scribing_price" />
					<view class="unit">元</view>
				</view>


				<view class="item flex-row-center">
					<view class="title">最低售价 </view>

					<input class="qrinput" placeholder="请输入" placeholder-class="input-hint" type="digit"
						v-model="minimum_price" />
					<view class="unit">元</view>
				</view>



				<view class="item flex-row-center" @click="$navigateTo('/pages/goods/goods_label?selectMode=true')">
					<view class="title">商品标签 </view>
					<view class="value">{{goods_label.lable_name}}
						<image src="../../static/arrow-blackmin.png" mode="heightFix"></image>
					</view>
				</view>
				<view class="item flex-row-center" @click="$navigateTo('/pages/goods/goods_brand?selectMode=true')">
					<view class="title"> 商品品牌</view>
					<view class="value">{{brand.brand_name}}
						<image src="../../static/arrow-blackmin.png" mode="heightFix"></image>
					</view>
				</view>

				<view class="item flex-row-center">
					<view class="title">规格</view>

					<input placeholder="请输入" placeholder-class="input-hint" v-model="specifications" />
				</view>
				<view class="item flex-row-center">
					<view class="title">商品简介 </view>

					<input placeholder="请输入" placeholder-class="input-hint" v-model="goods_introduce" />
				</view>
				<view class="item flex-row-center">
					<view class="title">助记码 </view>

					<input placeholder="请输入" placeholder-class="input-hint" v-model="mnemonic_code" />
				</view>
				<view class="item flex-row-center">
					<view class="title">保质天数</view>

					<input placeholder="请输入" placeholder-class="input-hint" v-model="quality_guarantee_num" />
				</view>
				<view class="item flex-row-center">
					<view class="title">产地</view>

					<input placeholder="请输入" placeholder-class="input-hint" v-model="place_of_origin" />
				</view>
			</view>

		</scroll-view>
		<view class="bottom-container flex-row-center" v-if="goods_data">
			<view class="item" @click="dialogShow=true">删除</view>
			<view class="item1" @click="goPrint()">打印标签</view>
		</view>

		<alert :show="dialogShow" message="是否删除!" @leftAction="dialogShow=false" @rightAction="del">
		</alert>
	</view>
</template>

<script>
	import dsBridge from 'dsbridge'
	import request from '@/common/api/request.js'
	export default {

		data() {
			return {
				vipInputShow:true,
				dialogShow: false,
				title: '新增商品',
				extensionShow: false,
				imgs: [],
				imgIds: [],
				splxList: [{
					name: "普通",
					check: true
				}, {
					name: "套餐"
				}],
				jjfsList: [{
					name: "普通",
					check: true
				}, {
					name: "称重"
				}, {
					name: "计数"
				}],
				smfsList: [{
					name: "上架",
					check: true
				}, {
					name: "下架"
				}, {
					name: "售罄"
				}],
				cashier_setup: [{
					name: "收银时允许积分"
				}, {
					name: "收银时允许改价"
				}, {
					name: "收银时允许折扣"
				}],

				isVipPrice: false,
				vipPriceData: {
					level0: 0,
					level1: 0,
					level2: 0,
				},

				"goods_barcode": "",
				"goods_classify": {},
				"goods_name": "",
				"initial_stock": "",
				"sale_price": "",
				"purchase_price": "",
				"goods_unit": {},
				"supplier": {},
				"allow_member_price": "不允许",
				"sale_state": "下架",
				"valuation_mode": "称重",
				"goods_type": "普通",
				"allow_min_sales_price": "不允许",
				"scribing_price": "",
				"minimum_price": "",
				"specifications": "",
				"goods_label": {},
				"brand": {},
				"goods_introduce": "",
				"mnemonic_code": "",
				"quality_guarantee_num": "",
				"place_of_origin": "",
				"manufacture_date": "1950-01-01",
				"vipPriceData": [{
					"discount_no": "ZK202210260019",
					"member_level_no": "DJ202210260035",
					"member_level_name": "普通会员",
					"level": "1",
					"discount_rate": 100,
					member_discount_amount: ""
				}, {
					"discount_no": "ZK202210260020",
					"member_level_no": "DJ202210260036",
					"member_level_name": "银卡等级",
					"level": "2",
					"discount_rate": 95,
					member_discount_amount: ""
				}, {
					"discount_no": "ZK202210260021",
					"member_level_no": "DJ202210260037",
					"member_level_name": "金卡等级",
					"level": "3",
					"discount_rate": 90,
					member_discount_amount: ""
				}],

				setMeal: [], //套餐
				oldSetMeal: [], //修改的时候用
				oldSetMealMap: {}, //修改的时候用
				goods_data: null,
				classifycations: null
			}

		},
		onLoad(options) {
			if(!options.add){
				this.goods_data = uni.getStorageSync("goods_edit_data");
			}
			uni.setStorageSync("goods_edit_data", null)
			if (this.goods_data != null) {
				this.title = "编辑商品" 
				if (this.goods_data.goods_image) {
					var token = dsBridge.call("getToken");
					if (token == '' || token == undefined) {
						token = uni.getStorageSync("token") ?? ''
					}
					this.imgs.push(
						`http://139.196.209.46:4882/file/download?fileNo=${this.goods_data.goods_image}&bx_auth_ticket=${token}`
					)

				}
				for (let i in this.splxList) {
					let item = this.splxList[i];
					item.check = item.name == this.goods_data.goods_type
				}
				for (let i in this.jjfsList) {
					let item = this.jjfsList[i];

					item.check = item.name == this.goods_data.valuation_mode

				}
				for (let i in this.smfsList) {
					let item = this.smfsList[i];
					item.check = item.name == this.goods_data.sale_state
				}
				this.isVipPrice = this.goods_data.allow_member_price == "允许"
				this.goods_name = this.goods_data.goods_name
				this.goods_barcode = this.goods_data.goods_barcode
				this.initial_stock = this.goods_data.initial_stock
				this.sale_price = this.goods_data.sale_price
				this.purchase_price = this.goods_data.purchase_price || 0
				this.minimum_price = (this.goods_data.minimum_price||'') + ''
				this.scribing_price = this.goods_data.scribing_price || ''
				this.specifications = this.goods_data.specifications
				this.goods_introduce = this.goods_data.goods_introduce
				this.mnemonic_code = this.goods_data.mnemonic_code
				this.quality_guarantee_num = this.goods_data.quality_guarantee_num
				this.place_of_origin = this.goods_data.place_of_origin
				this.allow_min_sales_price = this.goods_data.allow_min_sales_price


				let cs = this.goods_data.cashier_setup?.split(",");
				if(cs){
					for (let i in this.cashier_setup) {
						this.cashier_setup[i].check = cs.indexOf(this.cashier_setup[i].name) != -1
					}
				}

				this.getPackageDetail();
				this.getGoodsMember()
			}

			this.getClassifycations();
			this.getUnits();
			this.getSuppliers();
			this.getlabels();
			this.getBrand();
			this.getVipPrice();
		},

		onShow() {
			let list = uni.getStorageSync("setMealGoodsList");
			if (list != null) {
				this.setMeal = list;
				uni.setStorageSync("setMealGoodsList", null)
			}
		},
		watch: {

			sale_price(val) {
				for (var i = 0; i < this.vipPriceData.length; i++) {
					let item = this.vipPriceData[i];
					if (item && item.discount_rate) {
						if (val == '') {
							item.member_discount_amount = '';
						} else {
							// item.member_discount_amount = (((+val) * (+item.discount_rate)) / 100).toFixed(2) * 1;
						}
					}
				}
			}
		},

		methods: {
			format(e){
				
			},
			toMeal() {
				uni.setStorageSync("setMealGoodsList", this.setMeal);
				this.$navigateTo('/pages/goods/set_meal')
			},
			numberVerify(e) {
				var n = e.detail.value
				var s = ''
				if (n.indexOf("-") != -1)
					s = ''
				else
					s = n;
				console.log(s, "-", this.purchase_price)
				setTimeout(() => {
					this.purchase_price = s;
				}, 1)


			},
			goPrint() {
 				uni.setStorageSync("print_goods_no_list", [this.goods_data.goods_no])
				uni.setStorageSync("print_goods_list", [this.goods_data]);
				uni.navigateTo({
					url: '/pages/goods/goods_print'
				})
			},
			del() {
				uni.showLoading({
					title: "删除中..."
				})
				this.$apiPost("/retail/operate/srvretail_goods_info_delete", [{
					"serviceName": "srvretail_goods_info_delete",
					"condition": [{
						"colName": "id",
						"ruleType": "in",
						"value": this.goods_data.id
					}]
				}]).then(res => {
					uni.hideLoading()
					uni.setStorageSync("refresh", true)
					if (res.state == "SUCCESS") {
						uni.showToast({
							title: "成功！",
							mask: true
						})

						setTimeout(() => {
							uni.navigateBack()
						}, 1500)
					}
				})
			},
			initialStockInput: function(e) {
				this.initial_stock = e.detail.value.replace(/\D/g, '')
			},
			save() {
				let hint = null;
				if (this.goods_barcode.length == 0) {
					hint = "请输入条码"
				} else if (this.goods_name.trim().length == 0) {
					hint = "请输入商品名称"
				} else if (this.goods_classify.no == null) {
					hint = "请选择分类"
				} else if (this.initial_stock.length == 0) {
					hint = "请输入初始库存"
				} else if (this.sale_price.length == 0) {
					hint = "请输入销售价格"
				} else if (this.purchase_price.length == 0) {
					// hint = "请输入进货价"
				} else if (this.goods_unit.unit_no == null) {
					// hint = "请选择商品单位"
				} else if (this.supplier.supplier_no == null) {
					// hint = "请选择供应商"
				}

				if (parseFloat(this.initial_stock) < 0) {
					hint = "初始库存必须大于等于0！"
				}

				if (parseFloat(this.sale_price) < 0) {
					hint = "销售价格必须大于0！"
				}
				if (parseFloat(this.purchase_price) < 0) {
					hint = "进货价必须大于等于0！"
				}

				if (parseFloat(this.sale_price) < parseFloat(this.purchase_price)) {
					hint = "销售价格必须大于等于进货价格！"
				}

				if (this.scribing_price.length > 0 && parseFloat(this.scribing_price) <= 0) {
					hint = "划线价必须大于0！"
				}

				if (this.minimum_price != null && this.minimum_price.length > 0 && parseFloat(this.minimum_price) <= 0) {
					if(parseFloat(this.minimum_price)==0){
						this.minimum_price=''
					}else{
						hint = "最低销售价必须大于0！"
					}
				}
				if (this.allow_min_sales_price != '允许') {
					if (this.minimum_price != null && this.minimum_price.length > 0 && parseFloat(this.minimum_price) >
						parseFloat(this.sale_price)) {
						hint = "最低销售价必须小于售价！"
					}
				}

				if (hint != null) {
					uni.showToast({
						title: hint,
						icon: 'none'
					})
					return;
				}
				let goods_type = "";
				for (let i in this.splxList) {
					if (this.splxList[i].check == true) {
						goods_type = this.splxList[i].name
					}
				}
				let valuation_mode = "";
				for (let i in this.jjfsList) {
					if (this.jjfsList[i].check == true) {
						valuation_mode = this.jjfsList[i].name
					}
				}
				let _vipPriceData = [];
				if (this.isVipPrice) {
					for (let i in this.vipPriceData) {
						let ivp = this.vipPriceData[i];
						if (this.goods_data == null||(this.goods_data?.allow_member_price!='允许' && !this.vipPriceData.length)) {
							_vipPriceData.push({
								discount_no: ivp.discount_no,
								discount_rate: ivp.discount_rate,
								member_discount_amount: ivp.member_discount_amount,
								member_level_name: ivp.member_level_name,
								member_level_no: ivp.member_level_no
							})
						} else {
							_vipPriceData.push({
								serviceName: "srvretail_goods_member_update",
								condition: [{
									"colName": "id",
									"ruleType": "eq",
									"value": ivp.id,
								}],
								data: [{
									"member_discount_amount": ivp.member_discount_amount
								}],
								depend_keys: [{
									"type": "column",
									"depend_key": "goods_no",
									"add_col": "goods_no"
								}]
							})
						}
					}
				}


				let child_data_list = [];
				if (_vipPriceData.length > 0) {
					if (this.goods_data == null||(this.goods_data?.allow_member_price!='允许' && !this.vipPriceData.length)) {
						child_data_list.push({
							"data": _vipPriceData,
							"serviceName": "srvretail_goods_member_add",
							"depend_keys": [{
								"type": "column",
								"depend_key": "goods_no",
								"add_col": "goods_no"
							}]
						})
					} else {
						child_data_list = _vipPriceData
					}


				}
				///暂时解决修改 报错问题
				// if (this.goods_data != null) {
				// 	child_data_list = [];
				// }


				var sale_state = "";
				for (let i in this.smfsList) {
					if (this.smfsList[i].check == true) {
						sale_state = this.smfsList[i].name;
					}
				}
				var cashier_setup = "";
				for (let i in this.cashier_setup) {
					if (this.cashier_setup[i].check == true) {
						cashier_setup = cashier_setup + "," + this.cashier_setup[i].name;
					}
				}
				if (cashier_setup.length > 0)
					cashier_setup = cashier_setup.substring(1);

				if (this.splxList[1].check && this.setMeal.length > 0) {
					let mealdata = [];
					for (let i in this.setMeal) {
						let meal = this.setMeal[i]
						if (this.oldSetMealMap[meal.id]) continue //已经存在
						mealdata.push({
							"goods_barcode": meal.goods_barcode,
							"goods_name": meal.goods_name.trim(),
							// "package_goods_no": "SP202210290001",
							"package_goods_no": meal.goods_no,
							"goods_unit": meal.goods_unit,
							"goods_image": meal.goods_image,
							"goods_stock": meal.goods_stock,
							"goods_num": meal.goods_num,
							"sale_price": meal.sale_price
						})
					}
					child_data_list.push({
						"data": mealdata,
						"serviceName": "srvretail_package_detail_add",
						"depend_keys": [{
							"type": "column",
							"depend_key": "goods_no",
							"add_col": "goods_no"
						}]
					})

					//修改还需要处理 套餐删除，修改
					if (this.goods_data != null) {
						 
						 let newSetMealMap ={}
						 for (let i in this.setMeal) {
						 	newSetMealMap[this.setMeal[i].id] = this.setMeal[i].goods_num
						 }
						 
						for (let i in this.oldSetMeal) {
							let meal = this.oldSetMeal[i]
							if (newSetMealMap[meal.id] != null) {//存在
								if(newSetMealMap[meal.id]==meal.goods_num)continue//相等
								child_data_list.push({
									"data": [{
										"goods_num":newSetMealMap[meal.id],
									}],
									"serviceName": "srvretail_package_detail_update",
									condition: [{
										"colName": "id",
										"ruleType": "eq",
										"value": meal.id,
									}],
									"depend_keys": [{
										"type": "column",
										"depend_key": "goods_no",
										"add_col": "goods_no"
									}]
								})
							} else { //删除
								child_data_list.push({
									"serviceName": "srvretail_package_detail_delete",
									condition: [{
										"colName": "id",
										"ruleType": "eq",
										"value": meal.id,
									}],
									"depend_keys": [{
										"type": "column",
										"depend_key": "goods_no",
										"add_col": "goods_no"
									}]
								})
							}

						}



					}

				}
				let params = {
					goods_barcode: this.goods_barcode,
					goods_name: this.goods_name.trim(),
					goods_classify: this.goods_classify.no,
					initial_stock: this.initial_stock,
					sale_price: this.sale_price,
					purchase_price: this.purchase_price||0,
					goods_unit: this.goods_unit.unit_no,
					supplier: this.supplier.supplier_no,
					goods_type: goods_type,
					valuation_mode: valuation_mode,
					allow_member_price: this.isVipPrice ? '允许' : '不允许',
					child_data_list: child_data_list,
					sale_state: sale_state,
					cashier_setup: cashier_setup,
					// allow_min_sales_price: this.extensionShow ? '允许' : '不允许',allow_min_sales_price
					allow_min_sales_price: this.allow_min_sales_price,
					scribing_price: this.scribing_price || 0,
					minimum_price: this.minimum_price || 0,
					goods_label: this.goods_label?.lable_no,
					brand: this.brand?.brand_no,
					specifications: this.specifications,
					goods_introduce: this.goods_introduce,
					mnemonic_code: this.mnemonic_code,
					quality_guarantee_num: this.quality_guarantee_num || 0,
					place_of_origin: this.place_of_origin,
					channel_source: this.$SystemType
				};
				if (this.imgIds.length > 0) {
					params.goods_image = this.imgIds[0];
				}
				let url = this.goods_data == null ? "/retail/operate/srvretail_goods_info_add" :
					"/retail/operate/srvretail_goods_info_update";
				let addparams = [{
					"serviceName": "srvretail_goods_info_add",
					"condition": [],
					"data": [params]
				}]
				let updateparams = [{
					"serviceName": "srvretail_goods_info_update",
					"condition": [{
						"colName": "id",
						"ruleType": "eq",
						"value": this.goods_data?.id
					}],
					"data": [params]
				}]
				uni.showLoading({
					title: "加载中...",
					mask: true
				})
				this.$apiPost(url, this.goods_data == null ? addparams :
					updateparams).then(res => {
					uni.hideLoading()
					if (res.state == "SUCCESS") {
						uni.showToast({
							title: "成功！",
							mask: true
						})
						uni.setStorageSync("addGoodsSuccess", true)
						setTimeout(() => {
							uni.navigateBack()
							if (getCurrentPages().length == 1)
								dsBridge.call("back")
						}, 1500)
					}

				})
			},
			autoBarcode() {
				this.goods_barcode = new Date().getTime();
			},
			goodsClassifySelect() {
				uni.navigateTo({
					url: '/pages/goods/classify_goods?selectMode=true'
				})
			},
			getClassifycations() {
				let conditionArr = [{"colName":"classify_name","ruleType":"like","value":"临时分类"}];
				if(this.goods_data){
					conditionArr = [];
				}

				this.$apiPost("/retail/select/srvretail_goods_classify_select", {
					serviceName: "srvretail_goods_classify_select",
					"colNames": ["*"],
					"condition": conditionArr,
				}).then(res => {
					this.classifycations = res.data;
					if (this.goods_data != null) {
						for (let i in this.classifycations) {
							if (this.goods_data.goods_classify == this.classifycations[i].no) {
								this.goods_classify = this.classifycations[i]
							}
						}
					} else {
						this.goods_classify = this.classifycations[0]
					}
				})
			},
			getUnits() {
				let conditionArr = [{"colName":"unit_name","ruleType":"like","value":"临时单位"}];
				if(this.goods_data){
					conditionArr = [];
				}
				this.$apiPost("/retail/select/srvretail_store_unit_select", {
					"serviceName": "srvretail_store_unit_select",
					"colNames": ["*"],
					"condition": conditionArr,
					"order": [],
					"page": {
						"pageNo": 1,
						"rownumber": 30
					}
				}).then(res => {
					if (this.goods_data != null) {
						for (let i in res.data) {
							if (this.goods_data.goods_unit == res.data[i].unit_no) {
								this.goods_unit = res.data[i]
							}
						}
					} else {
						this.goods_unit = res.data[0]
					}
				})
			},
			getSuppliers() {
				let conditionArr = [{"colName":"supplier_state","ruleType":"eq","value":"正常","literalValue":true},{"colName":"supplier_name","ruleType":"like","value":"临时供应商"}];
				if(this.goods_data){
					conditionArr = [{
						"colName": "supplier_state",
						"ruleType": "eq",
						"value": "正常",
						"literalValue": true
					}]
				}
				this.$apiPost("/retail/select/srvretail_supplier_info_select", {
					"serviceName": "srvretail_supplier_info_select",
					"colNames": ["*"],
					"condition": conditionArr,
					"order": [],
					"page": {
						"pageNo": 1,
						"rownumber": 30
					}
				}).then(res => {
					if (this.goods_data != null) {
						for (let i in res.data) {
							if (this.goods_data.supplier == res.data[i].supplier_no) {
								this.supplier = res.data[i]
							}
						}
					} else {
						this.supplier = res.data[0]
					}
				})
			},
			getlabels() {
				let conditionArr = [{"colName":"lable_name","ruleType":"like","value":"临时标签"}];
				if(this.goods_data){
					conditionArr = [{
						"colName": "store_no",
						"ruleType": "like",
						"value": ""
					}];
				}
				this.$apiPost("/retail/select/srvretail_goods_lable_select", {
					"serviceName": "srvretail_goods_lable_select",
					"colNames": ["*"],
					"condition": conditionArr,
					"order": [],
					"page": {
						"pageNo": 1,
						"rownumber": 30
					}
				}).then(res => {
					if (this.goods_data != null) {
						for (let i in res.data) {
							if (this.goods_data.goods_label == res.data[i].lable_no) {
								this.goods_label = res.data[i]
							}
						}
					} else {
						this.goods_label = res.data[0]
					}
				})
			},
			getBrand() {
				let conditionArr = [{"colName":"brand_name","ruleType":"like","value":"临时品牌"}];
				if(this.goods_data){
					conditionArr = [{
						"colName": "store_no",
						"ruleType": "like",
						"value": ""
					}];
				}
				this.$apiPost("/retail/select/srvretail_goods_brand_select", {
					"serviceName": "srvretail_goods_brand_select",
					"colNames": ["*"],
					"condition": conditionArr,
					"order": [],
					"page": {
						"pageNo": 1,
						"rownumber": 30
					}
				}).then(res => {
					if (this.goods_data != null) {
						for (let i in res.data) {
							if (this.goods_data.brand == res.data[i].brand_no) {
								this.brand = res.data[i]
							}
						}
					} else {
						this.brand = res.data[0]
					}
				})
			},
			getVipPrice() {
				let condition = [];
				if (!(this.goods_data == null||(this.goods_data?.allow_member_price!='允许' &&  !this.vipPriceData.length)) ) {
					condition.push({
						"colName": "goods_no",
						"ruleType": "eq",
						"value": this.goods_data.goods_no
					})
				}
				let action = this.goods_data == null||(this.goods_data?.allow_member_price!='允许' &&  !this.vipPriceData.length) ? 'srvretail_aio_member_discount_setup_select' :
					'srvretail_goods_member_select'
				this.$apiPost(`/retail/select/${action}`, {
					"serviceName": action,
					queryMethod: "select",
					"colNames": ["*"],
					"condition": condition,
					"page": {
						"pageNo": 1,
						"rownumber": 30
					}
				}).then(res => {
					this.vipPriceData = res.data;

				})
			},
			getPackageDetail() {
				var ths=this
				this.$apiPost("/retail/select/srvretail_package_detail_select", {
					"serviceName": "srvretail_package_detail_select",
					"colNames": [
						"*"
					],
					"condition": [{
						"colName": "goods_no",
						"ruleType": "eq",
						"required": true,
						"value": this.goods_data.goods_no
					}]
				}).then(res => {
					this.setMeal = res.data
					ths.oldSetMeal = JSON.parse(JSON.stringify(this.setMeal))
 
					for (let i in this.oldSetMeal) {
						ths.oldSetMealMap[this.setMeal[i].id] = this.setMeal[i].goods_num
					}
					console.log("list",this.oldsetMeal)
				})
			},
			getGoodsMember() {
				this.$apiPost("/retail/select/srvretail_goods_member_select", {
					"serviceName": "srvretail_goods_member_select",
					"colNames": [
						"*"
					],
					"condition": [{
						"colName": "goods_no",
						"ruleType": "eq",
						"required": true,
						"value": this.goods_data.goods_no
					}]
				}).then(res => {
					this.vipPriceData = res.data
				})
			},
			switchVipPrice() {
				this.isVipPrice = !this.isVipPrice;
			},
			switchallow_min_sales_price() {
				if (this.allow_min_sales_price == '允许')
					this.allow_min_sales_price = '不允许';
				else
					this.allow_min_sales_price = '允许';
			},
			itemRadioCheck(item, list) {
				for (var i in list) {
					list[i].check = false;
				}
				item.check = true
			},
			itemCheckbox(item) {
				item.check = !(item.check ?? false)
				this.$forceUpdate()
			},
			imgsClick(current, urls) {
				uni.previewImage({
					current,
					urls
				})
			},
			qrcodeClick() {
				let ths = this;
				dsBridge.call("QRCode", {}, function(res) {
					if (res) {
						ths.searchStr = "" + res
						ths.goods_barcode = res

					}
				})
			},
			addImage() { //  事件名称  （自定义）
				// let count = 3 - this.imgs.length
				// uni.chooseImage({
				//     count:count ,                               //  上传图片数量的控制
				//     success(res){
				//       let tempfilepaths = res.tempfilepaths     //  图片的本地文件路径列表
				//       tempfilepaths.forEach((item,index) =>{
				//         if(index < count){             // 处理H5 端，多选的情况  （只有小于 count 才会被push 到数组里面）
				//           this.imgs.push({                 // push 对象到  imageLists[] 数组里面
				//             url:item
				//         	})
				//         }
				//       })
				//     }
				// })
				let ths = this;
				// uni.showLoading({
				// 	title: '加载中...'
				// })
				dsBridge.call("chooseImage", {
					count: 1, //数量
					sourceType: "", //不填全部 album 从相册选图，camera 使用相机
				}, function(res) {
					// uni.hideLoading()
					let data = JSON.parse(res)

					if (data.file_no) {
						var token = dsBridge.call("getToken");
						if (token == '' || token == undefined) {
							token = uni.getStorageSync("token") ?? ''
						}
						ths.imgs.push(
							`http://139.196.209.46:4882/file/download?fileNo=${data.file_no}&bx_auth_ticket=${token}`
						)
						// ths.imgs.push(data.local_path)
						ths.imgIds.push(data.file_no)
					}




					////本地h5 上传失败
					// request.uploadImages(ths.imgs, {
					// 	serviceName: "srv_bxfile_service",
					// 	interfaceName: "add",
					// 	app_no: "retail",
					// 	table_name: "bxretail_goods_info",
					// 	columns: "goods_image"
					// }).then(res => {
					// 	console.log(JSON.stringify(res))
					// 	if (res.statusCode == 200) {
					// 			let data=JSON.parse(res.data)
					// 			ths.imgIds.push(data.file_no)
					// 			alert(data.file_no);  
					// 	} else {
					// 		uni.showToast({
					// 			title: "上传失败",
					// 			icon: 'error'
					// 		})
					// 	}
					// })
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.apprightbtn {
		color: rgba(255, 255, 255, 1);
		font-size: 32rpx;
		padding: 0 50rpx;
	}

	.title {
		min-width: 188rpx;
		box-sizing: content-box;
		padding: 28rpx 0 24rpx 24rpx;

		.line1 {
			margin-right: 14rpx;
			width: 0rpx;
			height: 26rpx;
			border: 4rpx solid #38C09E;
			border-radius: 4rpx;
		}

		text {
			color: rgba(16, 16, 16, 1);
			font-size: 32rpx;
			font-weight: 500;
		}

		.show1 {
			margin-left: 12rpx;
			padding: 8rpx 14rpx;
			border-radius: 8rpx 0 0 8rpx;
			background: rgba(56, 192, 158, 1);
			border: 1px solid rgba(56, 192, 158, 1);
			font-size: 24rpx;
			font-weight: 500;
			color: white;
		}

		.hide1 {
			margin-left: 14rpx;
			font-size: 24px;
			font-weight: 500;
			color: white;
			font-size: 24rpx;
			font-weight: 500;
			color: black;
			padding: 8rpx 14rpx;
			border-radius: 8rpx 0 0 8rpx;
			background: rgba(255, 255, 255, 1);
			border: 1px solid rgba(229, 229, 229, 1);
		}

		.show2 {
			padding: 8rpx 14rpx;
			border-radius: 0 8rpx 8rpx 0;
			background: rgba(56, 192, 158, 1);
			border: 1px solid rgba(56, 192, 158, 1);
			font-size: 24rpx;
			font-weight: 500;
			color: white;
		}

		.hide2 {

			font-size: 24px;
			font-weight: 500;
			color: white;
			font-size: 24rpx;
			font-weight: 500;
			color: black;
			padding: 8rpx 14rpx;
			border-radius: 0 8rpx 8rpx 0;
			background: rgba(255, 255, 255, 1);
			border: 1px solid rgba(229, 229, 229, 1);
		}

	}



	.item-contianer {
		width: 710rpx;
		margin-left: 20rpx;
		border-radius: 10rpx;
		background-color: #FFFFFFFF;
		box-sizing: border-box;

		.imgs {
			margin-top: 32rpx;
			margin-right: 24rpx;
			margin-bottom: 22rpx;
			border-radius: 12rpx;
			box-sizing: content-box;
			position: relative;

			image {
				width: 160rpx;
				height: 160rpx;
				margin-left: 24rpx;
			}

			.img-del {
				top: -10rpx;
				right: -10rpx;
				position: absolute;
				width: 50rpx;
				height: 50rpx;
			}
		}

		.item {
			min-height: 100rpx;
			border-bottom: 2rpx solid #F5F5F7;
			padding-right: 28rpx;

			.check-box {}

			.title {
				color: rgba(16, 16, 16, 1);
				padding-left: 24rpx;
				padding-top: 24rpx;
				font-size: 32rpx;
				font-weight: 700;

				.x {
					color: #D43030;
					font-size: 32rpx;
				}
			}

			.mealedit {
				font-size: 30rpx;
				font-weight: 700;
				color: rgba(56, 192, 158, 1);
			}


			.value {
				flex: 1;
				text-align: right;
				font-size: 30rpx;
				font-weight: 400;
				color: rgba(51, 51, 51, 1);

				image {
					height: 16rpx;
					width: 8rpx;
				}
			}

			.unit {
				color: rgba(16, 16, 16, 1);
				font-size: 28rpx;
			}

			.qrinput {
				flex: 1;
				text-align: right;
				margin-right: 12rpx;
				color: rgba(51, 51, 51, 1);
				font-size: 30rpx;
				font-weight: 700;
			}

			.qrimg {
				width: 64rpx;
				height: 64rpx;

			}

			input {
				flex: 1;
				text-align: right;
				margin-right: 12rpx;
				color: rgba(51, 51, 51, 1);
				font-size: 30rpx;
				font-weight: 500;
			}

			.input-hint {
				font-size: 30rpx;
				font-weight: 500;
				color: rgba(153, 153, 153, 1);
			}



			.btn {
				padding: 0 8rpx;
				height: 54rpx;
				line-height: 54rpx;
				margin-left: 6rpx;
				border-radius: 12rpx;
				background: rgba(56, 192, 158, 1);
				color: rgba(255, 255, 255, 1);
				font-size: 30rpx;
				font-weight: 500;
				text-align: center;
			}

			.radio-group {
				flex-wrap: wrap;

				.radio {
					min-width: 150rpx;

					image {
						width: 24rpx;
						height: 24rpx;
						margin-right: 10rpx;
					}

					text {
						font-size: 30rpx;
						font-weight: 500;
						color: rgba(51, 51, 51, 1);
					}
				}
			}

			.check-box-group {
				padding-top: 28rpx;
				padding-bottom: 24rpx;

				.check-box1 {

					image {
						width: 24rpx;
						height: 24rpx;
						margin-right: 10rpx;
					}

					text {
						font-size: 28rpx;
						font-weight: 500;
						color: rgba(102, 102, 102, 1);
					}
				}
			}


		}

		.meal-box {
			padding-bottom: 24rpx;
			padding-right: 24rpx;
			min-height: 202rpx;
			width: 648rpx;
			margin-left: 28rpx;
			border: 2rpx solid rgba(224, 224, 224, 1);

			.empty {
				margin-top: 62rpx;
				margin-left: 138rpx;
				width: 376rpx;
				height: 80rpx;
				line-height: 80rpx;
				border-radius: 40rpx;
				text-align: center;
				border: 2rpx solid rgba(153, 153, 153, 1);
				font-size: 32rpx;
				font-weight: 700;
				color: rgba(0, 0, 0, 1);
			}

			.value {
				margin-top: 24rpx;
				margin-left: 16rpx;
				/** 文本1 */
				font-size: 28rpx;
				font-weight: 500;
			}
		}

		.item-vip {
			height: 100rpx;
			padding-right: 28rpx;


			.title {
				color: rgba(16, 16, 16, 1);
				padding-left: 24rpx;
				padding-top: 24rpx;
				font-size: 32rpx;
				font-weight: 700;

			}

			.value {
				flex: 1;
				text-align: right;
				font-size: 30rpx;
				font-weight: 400;
				color: rgba(51, 51, 51, 1);
			}


			input {
				flex: 1;
				text-align: right;
				margin-right: 12rpx;
				color: rgba(51, 51, 51, 1);
				font-size: 30rpx;
				font-weight: 500;
			}

			.input-hint {
				font-size: 30rpx;
				font-weight: 500;
				color: rgba(153, 153, 153, 1);
			}

			.vip-switch {
				width: 100rpx;
				height: 54rpx;
			}


		}

		.vip-price-box {
			margin: 0 28rpx 32rpx 28rpx;
			background-color: rgba(245, 245, 247, 1);
			box-sizing: content-box;
			border-right: 1rpx solid rgba(245, 245, 247, 1);
			border-bottom: 1rpx solid rgba(245, 245, 247, 1);

			.item1 {
				flex: 1;
				padding: 20rpx 0;
				line-height: 40rpx;
				width: 100%;
				text-align: center;
				font-size: 28rpx;
				font-weight: 500;
				color: rgba(0, 0, 0, 1);
				background-color: white;
				margin: 1rpx;
			}

			input {
				flex: 1;
				text-align: center;
				color: rgba(51, 51, 51, 1);
				font-size: 30rpx;
				min-height: 80rpx;
				font-weight: 500;
				background-color: white;
				margin: 1rpx;
			}

			.input-hint {
				font-size: 28rpx;
				font-weight: 500;
				padding: 20rpx 0;
				line-height: 40rpx;
				text-align: center;
				color: rgba(153, 153, 153, 1);
			}
		}

	}


	.bottom-container {
		background-color: white;
		box-shadow: 0 -16rpx 16rpx 1rpx #00000008;
		height: 146rpx;
		padding: 0 24rpx;
		justify-content: space-around;

		.item {
			border-radius: 40rpx;
			line-height: 80rpx;
			text-align: center;
			border: 2rpx solid #333333FF;
			width: 192rpx;
			height: 80rpx;
			font-size: 30rpx;
			font-weight: 500;

		}

		.item1 {
			border-radius: 40rpx;
			line-height: 80rpx;
			text-align: center;
			border: 2rpx solid rgba(56, 192, 158, 1);
			background-color: rgba(56, 192, 158, 1);
			color: white;
			width: 192rpx;
			height: 80rpx;
			font-size: 30rpx;
			font-weight: 500;
		}

		.text {
			flex: 1;
			margin-left: 60rpx;
			font-size: 30rpx;
			font-weight: 700;

		}

		.btn {
			margin-right: 34rpx;
			width: 192rpx;
			height: 80rpx;
			border-radius: 40rpx;
			background: rgba(56, 192, 158, 1);
			font-size: 30rpx;
			font-weight: 700;
			color: white;
			text-align: center;
			line-height: 80rpx;
		}
	}
</style>
